package com.alks.common.utils.export.service.util;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alks.common.service.RedisService;

import java.util.concurrent.TimeUnit;

/**
 * @author: chen
 * @date: 2025/1/13
 * @description: 导出任务工具类
 */
public class ExportTaskUtil {

    private static final String EXPORT_TASK_KEY = "export_task_key:";

    public static String startExportTask(String createUser, String originalName) {
        String[] split = originalName.split("\\.");
        String fileName = IdUtil.simpleUUID() + StrUtil.DOT + split[1];
        RedisService redisService = SpringUtil.getBean(RedisService.class);
        redisService.setHash(EXPORT_TASK_KEY + createUser, fileName, originalName);
        redisService.setExpireTime(EXPORT_TASK_KEY + createUser, 30, TimeUnit.MINUTES);
        return fileName;
    }

    public static String getOriginalName(String createUser, String fileName) {
        RedisService redisService = SpringUtil.getBean(RedisService.class);
        return redisService.getHash(EXPORT_TASK_KEY + createUser, fileName);
    }

    public static long existsExportTask(String createUser) {
        RedisService redisService = SpringUtil.getBean(RedisService.class);
        return redisService.hashSize(EXPORT_TASK_KEY + createUser);
    }

    public static void removeExportTask(String createUser, String fileName) {
        RedisService redisService = SpringUtil.getBean(RedisService.class);
        redisService.deleteHash(EXPORT_TASK_KEY + createUser, fileName);
        redisService.deleteIfHashEmpty(EXPORT_TASK_KEY + createUser);
    }
}
